package br.ufrpe.bsi.pvs.xml.response;

import java.util.ArrayList;
import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;

import br.ufrpe.bsi.pvs.model.roupa.Estilo;
import br.ufrpe.bsi.pvs.model.roupa.Genero;
import br.ufrpe.bsi.pvs.model.roupa.Roupa;
import br.ufrpe.bsi.pvs.model.roupa.Tamanho;
import br.ufrpe.bsi.pvs.model.roupa.TipoRoupa;
import br.ufrpe.bsi.pvs.xml.XMLUtil;

public class QueriedResponseProcess implements ResponseProcess {

	@Override
	public Object response(Element response) {
		List<Roupa> roupas = new ArrayList<>();

		for (Element roupaElement : response.getChildren()) {
			Roupa roupa = new Roupa();

			roupa.setId(Integer
					.parseInt(roupaElement.getChild("ID").getValue()));
			roupa.setDescricao(roupaElement.getChild("Descricao").getValue());
			roupa.setEstilo(Estilo.getEstilo(roupaElement.getChild("Estilo")
					.getValue()));
			roupa.setGenero(Genero.getGenero(Integer.parseInt(roupaElement
					.getChild("Genero").getValue())));
			roupa.setQuantEstoque(Integer.parseInt(roupaElement.getChild(
					"QuantEstoque").getValue()));
			roupa.setTamanho(Tamanho.getTamanho(roupaElement
					.getChild("Tamanho").getValue()));
			roupa.setTipoRoupa(TipoRoupa.getTipoRoupa(roupaElement.getChild(
					"TipoRoupa").getValue()));
			roupas.add(roupa);
		}

		return roupas;
	}

	/** generates a default add request xml document */
	public static Document getQueriedResponseDocument(String className) {
		Document document = new Document();

		Element root = new Element(XMLUtil.ELEM_ROOT);
		Element response = new Element(XMLUtil.ELEM_RESPONSE);

		Attribute responseName = new Attribute(XMLUtil.ATTR_NAME,
				XMLUtil.QUERIED);
		response.setAttribute(responseName);

		root.addContent(response);
		document.setRootElement(root);

		return document;
	}

}
